package com.vgmobile.cucrever2.mapstore;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.global.BaseActivity;

public class MapStoreActivity extends BaseActivity {
	private MapStoreActivity activity;
	private GoogleMap map;
	private GPSTracker mGpsTracker;
	private GPS mGps;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapstore_activity);
		activity = this;
		mGps = new GPS(this);
		mGpsTracker = new GPSTracker(this);
		map = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.mapstore_fragment)).getMap();
		map.setMyLocationEnabled(true);
		
		
//		map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
//
//		View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
//				.inflate(R.layout.layout_custom_infowindown, null);
//		TextView numTxt = (TextView) marker.findViewById(R.id.tvMapAddress);
//		numTxt.setText(title);
//		map.addMarker(new MarkerOptions().position(location).icon(
//				BitmapDescriptorFactory.fromBitmap(createDrawableFromView(this,
//						marker))));
//		double latitude = ResideMenuActivity.getLocationLatitude();
//		double longitude = ResideMenuActivity.getLocationLongitude();
//		LatLng location = new LatLng(latitude, longitude);
//		map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));

	}

	private Bitmap createDrawableFromView(Context context, View view) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);
		view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
		view.layout(0, 0, displayMetrics.widthPixels,
				displayMetrics.heightPixels);
		view.buildDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
				view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		view.draw(canvas);
		return bitmap;
	}

	private Route directions(final GeoPoint start, final GeoPoint dest) {
		Parser parser;
		String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";

		/**
		 * driving (default) indicates standard driving directions using the
		 * road network. walking requests walking directions via pedestrian
		 * paths & sidewalks (where available). bicycling requests bicycling
		 * directions via bicycle paths & preferred streets (where available).
		 * transit requests directions via public transit routes (where
		 * available).
		 */

		final StringBuffer sBuf = new StringBuffer(jsonURL);
		sBuf.append("origin=");
		sBuf.append(start.getLatitudeE6() / 1E6);
		sBuf.append(',');
		sBuf.append(start.getLongitudeE6() / 1E6);
		sBuf.append("&destination=");
		sBuf.append(dest.getLatitudeE6() / 1E6);
		sBuf.append(',');
		sBuf.append(dest.getLongitudeE6() / 1E6);
		sBuf.append("&sensor=true&mode=walking");
		parser = new GoogleParser(sBuf.toString());
		Route route = parser.parse();
		return route;
	}

	protected class ThreadDirectionsMap implements Runnable {
		private Thread thread;
		private GeoPoint pointStart, pointEnd;

		public ThreadDirectionsMap(LatLng pointStart, LatLng pointEnd) {
			this.pointStart = new GeoPoint((int) (pointStart.latitude * 1E6),
					(int) (pointStart.longitude * 1E6));
			this.pointEnd = new GeoPoint((int) (pointEnd.latitude * 1E6),
					(int) (pointEnd.longitude * 1E6));
			thread = new Thread(this, "Thread Directions Map");
			thread.start();
		}

		@Override
		public void run() {
			try {
				Route route = directions(pointStart, pointEnd);
				final List<GeoPoint> list = route.getPoints();
				if (!activity.isFinishing()) {
					activity.runOnUiThread(new Runnable() {
						public void run() {
							for (int i = 0; i < (list.size() - 1); i++) {
								addLine(list.get(i), list.get(i + 1));
							}
						}
					});
				}
			} catch (Exception e) {

			}
		}

		private void addLine(GeoPoint start, GeoPoint end) {
			map.addPolyline(new PolylineOptions()
					.add(new LatLng(start.getLatitudeE6() / 1E6, start
							.getLongitudeE6() / 1E6),
							new LatLng(end.getLatitudeE6() / 1E6, end
									.getLongitudeE6() / 1E6)).width(5)
					.color(Color.RED).geodesic(true));
		}
	}

	@Override
	protected void onResume() {
		mGps.startUpdates();
		mGpsTracker.getLocation();
		super.onResume();
	}

	@Override
	protected void onPause() {
		mGps.stopUpdates();
		super.onPause();
	}

	public double getLocationLatitude() {
		mGps.getLocation();
		double latitude;
		if (mGps.mLocation == null) {
			mGpsTracker.getLocation();
			latitude = mGpsTracker.getLatitude();
		} else {
			latitude = mGps.mLocation.getLatitude();
		}
		return latitude;
	}

	public double getLocationLongitude() {
		mGps.getLocation();
		double longitude;
		if (mGps.mLocation == null) {
			mGpsTracker.getLocation();
			longitude = mGpsTracker.getLongitude();
		} else {
			longitude = mGps.mLocation.getLongitude();
		}
		return longitude;
	}
}
